package com.spring.ai;

import com.spring.ai.bean.ChatMemoryDB;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@SpringBootTest
public class MongodbTest {
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 添加数据
     */
    @Test
    public void addData() {
        ChatMemoryDB  chatMemoryDB = mongoTemplate.save(ChatMemoryDB.builder().id("686c7c477315e60001c64cfa").content("你好").build());
        System.out.println(chatMemoryDB);
    }
    /**
     * 查询数据
     */
    @Test
    public void getData() {
        ChatMemoryDB chatMemoryDB = mongoTemplate.findById("686c7c477315e60001c64cfa", ChatMemoryDB.class);
        System.out.println("查询结果" + chatMemoryDB);
    }
    /**
     * 新增数据
     */
    @Test
    public void updateData() {
        Query query = new Query(Criteria.where("_id").is("686c7c477315e60001c64cfa"));
        Update update = new Update().set("content", "你好，我是小智1");
        mongoTemplate.updateFirst(query, update, ChatMemoryDB.class);

    }

    /**
     * 删除数据
     */
    @Test
    public void deleteData() {
        Query query = new Query(Criteria.where("_id").is("686c7c477315e60001c64cfa"));
        mongoTemplate.remove(query, ChatMemoryDB.class);
    }


}
